$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + '/systemLogs/listData.htm',
        datatype: "json",
        colModel: [			
			{ label: '操作人', name: 'userName',index: "user_name", sortable: true, width: 20 },
            { label: '日志内容', name: 'operation', sortable: false, width: 40 },
            { label: 'IP', name: 'ip', sortable: false, width: 30 },
            { label: '方法', name: 'method', sortable: false, width: 60 },
            { label: '参数', name: 'params', sortable: false, width: 110 ,formatter: function(value, options, row){
				if(!value){
					return "";
				}
				return "<pre class='jsonStyle' >" + syntaxHighlight(value) + "</pre>"; 
			}},
			{ label: '操作时间', name: 'createTime',index: "create_time", sortable: true, width: 40,formatter: function(value, options, row){
				if(value){
					return DateUtil.dateToStr("yyyy/MM/dd HH:mm:ss", new Date(value));
				}
				return "";
			}},
			{ label: '耗时(ms)', name: 'opTime', sortable: false, width: 20 }
        ],
        sort:{"create_time":"desc"},
        sortorder:'desc',
        sortname:'create_time',
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
    el:'#autocodeapp',
    data:{
        q:{
            userName: null,
            operation : null
        },
        showList: true,
        title:null,
        roleList:{},
        user:{
            status:1,
            deptId:null,
            deptName:null,
            roleIdList:[]
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'userName': vm.q.userName,"operation":vm.q.operation},
                page:page
            }).trigger("reloadGrid");
        }
    }
});


//json字符串格式化
function syntaxHighlight(json) {
    if (typeof json != 'string') {
        json = JSON.stringify(json, undefined, 2);
    }
    
    json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
    
    
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        var resultStr = '<label class="' + cls + '">' + match + '</label>';
        if(cls == 'key'){
        	resultStr = "<br>" + resultStr;
        }
        return resultStr;
    });
}